<!--
 * @author: sanbao
 * @Date: 2022-07-23 11:03:27
-->
<style lang='scss' scoped>
.my_table {
  .awrp {
    display: flex;
    padding: 10px 20px 10px 20px;
    box-sizing: border-box;
    border-top: 1px #ccc solid;
  }

  .awrp .left {
    flex: 1;
  }

  .awrp .right {
    flex: 4;
    .el-icon {
      margin-left: 10px;
      font-size: 20px;
    }
  }

  .warplist {
    display: flex;
  }

  .warpfeft {
    flex: 1;
  }

  .warpright {
    flex: 4;
  }
  .mx-1 {
    margin: 5px 10px;
  }
}
</style>

<template>
  <div class="my_table">
    <el-table
      :data="tableData"
      border
      max-height="500"
      style="width: 100%"
      :row-key="tableid"
      :tree-props="{ children: 'children' }"
    >
      <template v-for="(item, index) in tablelist">
        <!-- 序号 -->
        <el-table-column
          v-if="item.type == 'index'"
          :type="item.type"
          :label="item.label"
          width="60"
        >
        </el-table-column>
        <!-- 数据 -->
        <el-table-column
          v-else-if="!item.type"
          :prop="item.prop"
          :label="item.label"
          :width="item.width"
        >
        </el-table-column>
        <!-- 状态 -->
        <el-table-column
          v-else-if="item.type == 'mg_state'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <el-switch
              v-model="scope.row.mg_state"
              @click="handeltablebutton(scope.row.mg_state, scope.row.id)"
            />
          </template>
        </el-table-column>
        <!-- 分配角色结构 -->
        <el-table-column v-else-if="item.type == 'expand'" type="expand">
          <template #default="scope">
            <el-form
              label-position="left"
              inline
              class="demo-table-expand"
              v-if="scope.row.children"
            >
              <div class="awrp" v-for="(e, d) in scope.row.children" :key="d">
                <div class="left">
                  <el-button type="primary" plain>
                    {{ e.authName }}
                  </el-button>
                </div>
                <div class="right">
                  <div class="warplist" v-for="(v, i) in e.children" :key="i">
                    <div class="warpfeft">
                      <el-button type="success" plain>
                        {{ v.authName }}
                        <el-icon
                          @click.stop="handeltablebutton(item.action, v.id)"
                        >
                          <MilkTea />
                        </el-icon>
                      </el-button>
                    </div>
                    <div class="warpright">
                      <el-button
                        type="warning"
                        plain
                        v-for="(x, j) in v.children"
                        :key="j"
                      >
                        {{ x.authName }}
                        <el-icon
                          @click.stop="handeltablebutton(item.action, x.id)"
                        >
                          <MilkTea />
                        </el-icon>
                      </el-button>
                    </div>
                  </div>
                </div>
              </div>
            </el-form>
          </template>
        </el-table-column>
        <!-- paramsexpand -->
        <el-table-column v-else-if="item.type == 'paramsexpand'" type="expand">
          <template #default="scope">
            <el-tag
              v-for="(tag, index) in scope.row.attr_vals.split(' ')"
              :key="tag"
              class="mx-1"
              closable
              :disable-transitions="false"
              @close="handlebuttonmini(scope.row, index)"
            >
              {{ tag }}
            </el-tag>
            <input
              type="text"
              v-if="inputVisible"
              ref="InputRef"
              v-model="inputValue"
              class="ml-1 w-20"
              @blur="handleInputConfirm(scope.row, index)"
            />
            <el-button
              v-else
              class="button-new-tag ml-1"
              size="small"
              @click="showInput"
            >
              添加标签
            </el-button>
          </template>
        </el-table-column>
        <!-- level -->
        <el-table-column
          v-else-if="item.type == 'level'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <!-- <el-switch
              v-model="scope.row.mg_state"
              @click="handeltablebutton(scope.row.mg_state, scope.row.id)"
            /> -->
            <el-button type="primary" v-if="scope.row.level == '0'" plain>
              一级
            </el-button>
            <el-button type="success" v-else-if="scope.row.level == '1'" plain>
              二级
            </el-button>
            <el-button type="warning" v-else-if="scope.row.level == '2'" plain>
              三级
            </el-button>
          </template>
        </el-table-column>
        <!-- cat_deleted -->
        <el-table-column
          v-else-if="item.type == 'cat_deleted'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <el-icon v-if="scope.row.cat_deleted == false">
              <CircleCheckFilled />
            </el-icon>
            <el-icon v-else>
              <CircleCloseFilled />
            </el-icon>
          </template>
        </el-table-column>
        <!-- cat_level -->
        <el-table-column
          v-else-if="item.type == 'cat_level'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <el-button
              type="primary"
              v-if="scope.row.cat_level == 0"
              disabled
              plain
            >
              一级
            </el-button>
            <el-button
              type="success"
              v-if="scope.row.cat_level == 1"
              disabled
              plain
            >
              二级
            </el-button>
            <el-button
              type="warning"
              v-if="scope.row.cat_level == 2"
              plain
              disabled
            >
              三级
            </el-button>
          </template>
        </el-table-column>
        <!-- pay_status -->
        <el-table-column
          v-else-if="item.type == 'pay_status'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <!-- <el-switch
              v-model="scope.row.mg_state"
              @click="handeltablebutton(scope.row.mg_state, scope.row.id)"
            /> -->
            <el-button type="primary" v-if="scope.row.pay_status == '1'" plain>
              以付款
            </el-button>
            <el-button
              type="success"
              v-else-if="scope.row.pay_status == '0'"
              plain
            >
              未付款
            </el-button>
          </template>
        </el-table-column>
        <!-- order_pay -->
        <el-table-column
          v-else-if="item.type == 'order_pay'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <!-- <el-switch
              v-model="scope.row.mg_state"
              @click="handeltablebutton(scope.row.mg_state, scope.row.id)"
            /> -->
            <span v-if="scope.row.pay_status == '0'"> 否 </span>
            <span v-else-if="scope.row.pay_status == '1'"> 是 </span>
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          v-else-if="item.type == 'action'"
          :prop="item.prop"
          :label="item.label"
        >
          <template #default="scope">
            <el-button
              size="small"
              v-for="(v, i) in item.list"
              :key="i"
              :type="v.type"
              @click="
                handeltablebutton(
                  v.action,
                  scope.row.attr_id ||
                    scope.row.id ||
                    scope.row.goods_id ||
                    scope.row.cat_id
                )
              "
            >
              {{ v.name }}
            </el-button>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      v-if="pager"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pager.currentPage"
      :page-sizes="pager.pageSizes"
      :page-size="pager.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pager.total"
    >
    </el-pagination>
  </div>
</template>

<script lang='ts'>
import {
  defineComponent,
  ref,
  reactive,
  toRefs,
  computed,
  nextTick,
} from "vue";
import { ElInput } from "element-plus";
export default defineComponent({
  props: {
    tableData: {
      type: Array,
      default: [],
    },
    tablelist: {
      type: Array,
      default: [],
    },
    tableid: String,
    // 分页
    pager: {
      type: Object,
    },
  },
  components: {},
  setup(props, { emit }) {
    const handeltablebutton = (action: any, id: number) => {
      emit("handeltablebutton", { action, id });
    };
    const handlebuttonmini = (obj: any, index: number) => {
      let attr_vals = obj.attr_vals.split(" ");
      attr_vals.splice(index, 1);

      let object = {
        attr_name: obj.attr_name || "",
        attr_sel: obj.attr_sel || "",
        attr_vals: attr_vals.join(" ") || "",
      };
      emit("handlebuttonmini", { object, id: obj.attr_id });
    };
    const handleSizeChange = (size: number) => {
      emit("handleSizeChange", size);
    };
    const handleCurrentChange = (page: number) => {
      emit("handleCurrentChange", page);
    };

    const inputValue = ref("");
    const inputVisible = ref(false);
    const InputRef = ref<InstanceType<typeof ElInput>>();
    const showInput = () => {
      inputVisible.value = true;
      // nextTick(() => {
      //   InputRef.value!.input!.focus();
      // });
    };

    const handleInputConfirm = (obj: any, index: number) => {
      let attr_vals = obj.attr_vals.split(" ");
      attr_vals.push(inputValue.value);
      let object = {
        attr_name: obj.attr_name || "",
        attr_sel: obj.attr_sel || "",
        attr_vals: attr_vals.join(" ") || "",
      };
      emit("handlebuttonmini", { object, id: obj.attr_id });

      inputVisible.value = false;
      inputValue.value = "";
    };
    return {
      handeltablebutton,
      handleSizeChange,
      handleCurrentChange,
      handlebuttonmini,
      showInput,
      handleInputConfirm,
      inputValue,
      inputVisible,
    };
  },
});
</script>
